package claybird.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import claybird.dao.HandicapDao;
import claybird.dao.PersonneDao;
import claybird.dao.ResultDao;
import claybird.dao.ShootDayDao;
import claybird.dao.TeamDao;
import claybird.dom.Handicap;
import claybird.dom.Person;
import claybird.dom.Result;
import claybird.dom.ShootDay;

@Controller
public class CloseYearController {
	@Autowired ShootDayDao shootDao;
	@Autowired TeamDao teamDao;
	@Autowired PersonneDao personDao;
	@Autowired ResultDao resultDao;
	@Autowired HandicapDao handicapDao;
	
	@RequestMapping("CloseYear/Request")
	public String ShowPassWordPage(){
		return "close_year_validation";
	}
	
	@RequestMapping("CloseYear/Validation")
	public ModelAndView CloseYear(@RequestParam("password") String password,
									@RequestParam("year") Short year){
		if(!password.equals("admincbc")){
			return new ModelAndView("close_year_validation","error","Mot de passe erroné...");
		}
		List<Person> listPerson = personDao.getAllPersonne();
		Handicap hand = new Handicap();
		List<ShootDay> listShoot = shootDao.getShootByYear(year);
		for(Person pers: listPerson){
			this.setEvolution(pers,listShoot);
			hand = new Handicap(pers,pers.getLastHandicap(),(short) (year+1));
			handicapDao.persist(hand);
			pers.getListHandicap().add(hand);
		}
		return ShowResultYear(year);
	}
	
	
	@RequestMapping("CloseYear/ShowResult")
	public ModelAndView ShowResultYear(@RequestParam("year")int year){
		ModelAndView mv = new ModelAndView("year_result");
		List<Person> listPerson = this.getListInOrder(personDao.getAllPersonneWithoutGuest(),year);
		List<ResultList> listValidInOrder = this.getListInRankingOrder(listPerson, year);
		mv.addObject("year",year);
		mv.addObject("list",listValidInOrder);
		mv.addObject("listShootOfTheYear",shootDao.getShootByYear(year));
		return mv;
	}
	
	private List<ResultList> getListInRankingOrder(List<Person> listPerson,
			int year) {
		List<ResultList> listResult = new ArrayList<ResultList>();
		for (Person pers : listPerson){
			listResult.add(new ResultList(pers,year));
		}
		return listResult;
	}

	private List<Person> getListInOrder(List<Person> allPersonne,int year) {
		List<Person> listInOrder = new ArrayList<Person>();
		for(Person pers : allPersonne){
			boolean added = false;
			for(Person compare : listInOrder){
				if (pers.getLastHandicap(year).getCurrentHandicap()>compare.getLastHandicap( year).getCurrentHandicap()){
					listInOrder.add(listInOrder.indexOf(compare), pers);
					added = true;
					break;
				}
			}
			if (!added){
				listInOrder.add(pers);
			}
		}
		return listInOrder;
	}
	public class ResultList{
		private Person person;
		private boolean first=false;
		
		public ResultList(Person person, int year){
			this.person = person;
			this.first = personDao.isFirst(person,(short)year);
		}
		public Person getPerson() {
			return person;
		}

		public void setPerson(Person person) {
			this.person = person;
		}

		public boolean isFirst() {
			return first;
		}
		public void setFirst(boolean isFirst) {
			this.first = isFirst;
		}
		
	}
	private void setEvolution(Person pers,List<ShootDay> listShoot) {
		// TODO Auto-generated method stub
		Result lastDayResult=null;
		for(ShootDay day: listShoot){
			Result newResult = pers.getResultFor(day);
			if (newResult.getNumberOfClayShooted()>0){
				if (lastDayResult !=null){
					newResult.setEvolution(newResult.getRealValue()*2/lastDayResult.getPercentNewHandicap()*100);
					newResult.setPercentNewHandicap((float) ((lastDayResult.getPercentNewHandicap()*0.8)
							+(newResult.getNumberOfClayShooted()*newResult.getNumberOfClayTotal()/50/2.5)));
				}else{
					newResult.setEvolution(newResult.getRealValue()*2/pers.getLastHandicap().getCurrentHandicap()*100);
					newResult.setPercentNewHandicap((float) ((pers.getLastHandicap().getPreviousHandicap()*0.8)
							+(newResult.getNumberOfClayShooted()*newResult.getNumberOfClayTotal()/50/2.5)));
				}
				lastDayResult=newResult;
				pers.getLastHandicap().setCurrentHandicap(lastDayResult.getPercentNewHandicap());
				resultDao.persist(newResult);
				pers.getLastHandicap().setCurrentHandicap(lastDayResult.getPercentNewHandicap());
				pers.getLastHandicap().setFinalRanking(pers.getTotalRanking(pers.getLastHandicap().getYear()));
				personDao.persist(pers);
			}
		}
	}

}
